package com.ybkj.o2o.zhuanche.amqp.rabbitTemplate;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;

/**
 * 消息队列模板：用于发送和接受消息
 *
 * @author jacklee
 */
public class BaseTemplate {
    //需修改为对应主项目中存在的MQ配置文件中IP
    @Value("${spring.rabbitmq.host}")
    private String HOST ;
    //需修改为对应主项目中存在的MQ配置文件中MQ名
    @Value("${spring.rabbitmq.username}")
    private  String USERNAME;
    //需修改为对应主项目中存在的MQ配置文件中密码
    @Value("${spring.rabbitmq.password}")
    private  String PASSWORD;
    //需修改为对应主项目中存在的MQ配置文件中端口号(如下所示)
    private static final int PORT = 5672;
    //需修改为对应主项目中存在的MQ配置文件中amqp.sp.exchange
    protected static final String EXCHANGR_NAME = "xiaoka.push.exchange";

    private Connection connection;

    protected Channel channel;
    @PostConstruct
    public void init(){
        try {
            // Create a connection factory
            ConnectionFactory factory = new ConnectionFactory();

            // hostname of your rabbitmq server
            factory.setHost(HOST);
            factory.setUsername(USERNAME);
            factory.setPassword(PASSWORD);
            factory.setPort(PORT);

            connection = factory.newConnection();

            // creating a channel
            channel = connection.createChannel();
            channel.exchangeDeclare(EXCHANGR_NAME, "fanout", true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public BaseTemplate() {


    }
}
